home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
admin
/
clnwtmp.1
/
clnwtmp
/
clnwtmp-1.0
/
clnwtmp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-25
|
4KB
|
167 lines
/* clnwtmp.c Oct. 95
Mark Santelli
Reads in records from the wtmp file and writes them out if the
date from wtmp is greater than the date received in the command
line. The time of '00:00:00' is used in the date spec. The
wtmp or utmp filename is passed as the first parameter to the
program and the cutoff date is passed as the second. The new
system file is created in the current directory.
*/
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <utmp.h>
#include <fcntl.h>
#define BLOCK 140
struct utmp filebuf[BLOCK];
struct utmp outbuf [BLOCK];
time_t time_threshold;
int infile_handle, outfile_handle;
int OpenFiles(char *);
int ValidInput(int, char * []);
void ConvertDate(int, int, int);
void main(int argcnt, char *args[])
{
int bytes, sub, outsub = 0;
/* validate input */
if (!ValidInput(argcnt, args))
exit(1);
if (!OpenFiles(args[1]))
exit(1);
while ((bytes = read(infile_handle, &filebuf, sizeof(filebuf))) > 0)
{
bytes /= sizeof(struct utmp);
for (sub = 0; sub < bytes; sub++)
{
if (filebuf[sub].ut_time > time_threshold)
outbuf[outsub++] = filebuf[sub];
}
if (write(outfile_handle, &outbuf, sizeof(struct utmp) * outsub)
!= sizeof(struct utmp) * outsub)
{
perror("write error.");
exit(1);
}
outsub = 0;
}
exit(0);
}
int ValidInput(int parmcnt, char * parms[])
{
int day, mon, yr, check;
struct stat st;
if (parmcnt < 3) /* correct # of parms */
{
printf("\nusage: %s [/path/wtmp YYMMDD]\t where:\n", parms[0]);
printf("\n- '/path/wtmp' is the pathname of the system login file\n");
printf("- YYMMDD is the cutoff date\n");
printf("\tYY must be in the range 01-99\n");
printf("\tMM must be in the range 01-12\n");
printf("\tDD must be in the range 01-31\n");
printf("\nCreates a new system file not containing the records whose\n");
printf("dates are less than the date passed on the command line.\n");
printf("'utmp' can also be rewritten.\n");
return 0;
}
/* does file exists */
if (stat(parms[1], &st) == -1)
{
perror("\nstat error. ");
return 0;
}
/* date validation */
if (strlen(parms[2]) == 6)
{ /* is date numeric */
for (check = 0; check < 6; check++)
{
if (parms[2][check] < '0' || parms[2][check] > '9')
{
printf("\ndate contains non-numeric data\n");
return 0;
}
}
/* test ranges */
day = atoi(&parms[2][4]);
parms[2][4] = '\0';
if (day < 1 || day > 31)
{
printf("\nday must be an integer from (01 <--> 31)\n");
return 0;
}
mon = atoi(&parms[2][2]) - 1;
parms[2][2] = '\0';
if (mon < 0 || mon > 11)
{
printf("\nmonth must be an integer from (01 <--> 12)\n");
return 0;
}
yr = atoi(parms[2]);
if (yr < 1 || yr > 99)
{
printf("\nyear must be an integer from (01 <--> 99)\n");
return 0;
}
/* convert date received into */
ConvertDate(day, mon, yr); /* a time_t format like in wtmp */
}
else
{
printf("\ndate must be in YYMMDD format\n");
return 0;
}
return 1;
}
void ConvertDate(int d, int m, int y)
{
struct tm cutdate;
cutdate.tm_sec = 0;
cutdate.tm_min = 0;
cutdate.tm_hour = 0;
cutdate.tm_mday = d;
cutdate.tm_mon = m;
cutdate.tm_year = y;
cutdate.tm_isdst = -1;
time_threshold = mktime(&cutdate);
}
int OpenFiles(char * path)
{
if ((infile_handle = open(path, O_RDONLY )) == -1)
{
perror("\nerror on open 'wtmp'\n");
return 0;
}
if ((outfile_handle = open("new.wtmp", O_WRONLY | O_CREAT |
O_TRUNC, S_IWRITE)) == -1)
{
perror("\nerror on open 'new.wtmp'\n");
return 0;
}
return 1;
}